package com.casic.modules.gen.controller;

import com.casic.common.base.entity.PageEntity;
import com.casic.common.utils.Constant;
import com.casic.common.utils.PageUtils;
import com.casic.common.utils.R;
import com.casic.modules.gen.entity.SysTableInfoEntity;
import com.casic.modules.gen.service.SysGeneratorService;
import com.casic.modules.sys.entity.SysUserEntity;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.casic.common.utils.Query;
import com.casic.common.xss.XssHttpServletRequestWrapper;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 代码生成器
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年12月19日 下午9:12:58
 */
@Controller
@RequestMapping("/sys/generator")
public class SysGeneratorController {
	@Autowired
	private SysGeneratorService sysGeneratorService;

	@Autowired
	private RedisTemplate redisTemplate;
	
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	@RequiresPermissions("sys:generator:list")
	public R list(@RequestParam Map<String, Object> params){
//		//查询列表数据
//		Query query = new Query(params);
//		List<Map<String, Object>> list = sysGeneratorService.queryList(query);
//		int total = sysGeneratorService.queryTotal(query);
//
//		PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
//
//		return R.ok().put("page", pageUtil);


		int currentPage=1;
		int pageSize=10;
		if(params.get("page")!=null){
			currentPage=Integer.parseInt((String)params.get("page"));
		}
		if(params.get("limit")!=null){
			pageSize=Integer.parseInt((String)params.get("limit"));
		}
		PageHelper.startPage(currentPage, pageSize);
		List<SysTableInfoEntity> list=sysGeneratorService.queryList(params);
		return R.ok().put("page", new PageEntity<SysTableInfoEntity>(list));
	}
	
	/**
	 * 生成代码
	 */
	@RequestMapping("/code")
	@RequiresPermissions("sys:generator:code")
	public void code(HttpServletRequest request, HttpServletResponse response) throws IOException{
		//获取表名，不进行xss过滤
		HttpServletRequest orgRequest = XssHttpServletRequestWrapper.getOrgRequest(request);
		String tables = orgRequest.getParameter("tables");
		String module = orgRequest.getParameter("module");
		String author = orgRequest.getParameter("author");
		String[] tableNames = new Gson().fromJson(tables, String[].class);
		byte[] data = sysGeneratorService.generatorCode(tableNames,module,author);
		response.reset();  
        response.setHeader("Content-Disposition", "attachment; filename=\"casic-generator.zip\"");
        response.addHeader("Content-Length", "" + data.length);  
        response.setContentType("application/octet-stream; charset=UTF-8");  
  
        IOUtils.write(data, response.getOutputStream());
		if(Constant.REDIS_OPEN){
			redisTemplate.execute(new RedisCallback() {
				public String doInRedis(RedisConnection connection) throws DataAccessException {
					connection.flushDb();
					return "ok";
				}
			});
		}
	}

	/**
	 * 清除缓存
	 * @return
	 */
	@RequestMapping("/clearCache")
	@ResponseBody
	public R clearCache(){
		//判断是否使用redis
		if(Constant.REDIS_OPEN){
			redisTemplate.execute(new RedisCallback() {
				public String doInRedis(RedisConnection connection) throws DataAccessException {
					connection.flushDb();
					return "ok";
				}
			});
		}
		return R.ok();
	}
}
